<?php
namespace app\controller;

use app\BaseController;
use iszsw\curd\lib\Manage;
use think\facade\Config;

class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }

    public function tables($tableName='teacher')
    {
        $appName = 'base';

        $tablePath = Config::get('curd.engine.file.save_path');
        if (!is_dir($tablePath)) {
            mkdir($tablePath, 0777, true);
        }
        $tablePath= $tablePath . 'model';
        if (!is_dir($tablePath)) {
            mkdir($tablePath, 0777, true);
        }
        $list = Manage::instance()->tables($tableName);
        foreach($list as $table)
        {
            $tableName = ucfirst($table['table']);  

            //生成model文件
            $file = $tablePath .'\\' . $tableName .'Model.php';
            $string = '';
            $string .= '<?php' . "\n";
            $string .= 'declare (strict_types = 1);' . "\n";
            $string .= "\n";
            $string .= "namespace app\\". $appName ."\\model;" . "\n";
            $string .= "\n";
            $string .= 'use think\Model;' . "\n";
            $string .= "\n";
            $string .= 'class '. $tableName .'Model extends Model' . "\n";
            $string .= '{' . "\n";
            $string .= '    protected $name = \''. $table['table'] .'\';' . "\n";
            $string .= '    protected $schema = [' . "\n";
            $string .= "\n";
            $string .= '    ];' . "\n";
            $string .= '}' . "\n";

            if ($handle = fopen($file, 'w')) {
                fwrite($handle, $string);
                fclose($handle);
            } else {
                throw new \think\Exception(__('File {:file} does not have {:type} permission', ['file'=>$file, 'type' => 'write']), '500');
            }

            $file = $tablePath .'\\' . $tableName .'Validate.php';
            $string = "";
            $string .= "<?php\n";
			$string .= "namespace app\\". $appName ."\\validate;\n";
			$string .= "\n";
			$string .= "use think\Validate;\n";
			$string .= "use app\\". $appName ."\\service\\". $tableName ."Service;\n";
			$string .= "\n";
			$string .= "class ".$tableName ."Validate extends Validate\n";
			$string .= "{\n";
			$string .= "    protected \$rule = [\n";
			$string .= "        'id'          => ['require', 'checkId'],\n";
			$string .= "        'name'        => ['require'],\n";
			$string .= "    ];\n";
			$string .= "	\n";
			$string .= "    protected \$message = [\n";
			$string .= "        'id.require' => '缺少参数：ID',\n";
			$string .= "        'name.require'   => '请输入名称',\n";
			$string .= "    ];\n";
			$string .= "	\n";
			$string .= "    protected \$scene = [\n";
			$string .= "        'id'     => ['id'],\n";
			$string .= "        'info'   => ['id'],\n";
			$string .= "        'add'    => ['name'],\n";
			$string .= "        'edit'   => ['id', 'name'],\n";
			$string .= "        'del'    => ['id'],\n";
			$string .= "    ];\n";
			$string .= "	\n";
			$string .= "    protected function checkId(\$value, \$rule, \$data = [])\n";
			$string .= "    {\n";
			$string .= "        \$id = \$value;\n";
			$string .= "        \$data = ".$tableName ."Service::info(\$id);\n";
			$string .= "        return true;\n";
			$string .= "    }\n";
			$string .= "}\n";
			$string .= "\n";
            
            if ($handle = fopen($file, 'w')) {
                fwrite($handle, $string);
                fclose($handle);
            } else {
                throw new \think\Exception(__('File {:file} does not have {:type} permission', ['file'=>$file, 'type' => 'write']), '500');
            }

            $file = $tablePath .'\\' . $tableName .'Service.php';
            $string = "";

            $string .= "<?php\n";
			$string .= "namespace app\\". $appName ."\\service;\n";
			$string .= "use think\\facade\\Db;\n";
			$string .= "use think\\facade\\Filesystem;\n";
			$string .= "\n";
			$string .= "class ". $tableName ."Service{\n";
			$string .= "    public static function list(\$where = [], \$page = 1, \$limit = 10,  \$order = [], \$field = '')\n";
			$string .= "    {\n";
			$string .= "        if (empty(\$field)) {\n";
			$string .= "            \$field = '";
            $first = 0;
            foreach($table['fields'] as $field)
            {
                if($first > 0) $string .= ",";
                $first++;
                $string .=  $field['field'];
            }       
			$string .= "';\n";
			$string .= "        }\n";
			$string .= "\n";
			$string .= "        if (empty(\$order)) {\n";
			$string .= "            \$order = ['sort' => 'asc'];\n";
			$string .= "        }\n";
			$string .= "\n";
			$string .= "        \$count = Db::name('". $table['table'] ."')\n";
			$string .= "            ->where(\$where)\n";
			$string .= "            ->count('id');\n";
			$string .= "\n";
			$string .= "        \$list = Db::name('". $table['table'] ."')\n";
			$string .= "            ->field(\$field)\n";
			$string .= "            ->where(\$where)\n";
			$string .= "            ->page(\$page)\n";
			$string .= "            ->limit(\$limit)\n";
			$string .= "            ->order(\$order)\n";
			$string .= "            ->select()\n";
			$string .= "            ->toArray();\n";
			$string .= "\n";
			$string .= "        \$pages = ceil(\$count / \$limit);\n";
			$string .= "\n";
			$string .= "        \$data['count'] = \$count;\n";
			$string .= "        \$data['pages'] = \$pages;\n";
			$string .= "        \$data['page']  = \$page;\n";
			$string .= "        \$data['limit'] = \$limit;\n";
			$string .= "        \$data['list']  = \$list;\n";
			$string .= "\n";
			$string .= "        return \$data;\n";
			$string .= "    }\n";
			$string .= "\n";
			$string .= "    public static function info(\$id='')\n";
			$string .= "    {\n";
			$string .= "        \$where[] = ['id', '=',  \$id];\n";
			$string .= "        \$". $table['table'] ." = Db::name('". $table['table'] ."')\n";
			$string .= "            ->where(\$where)\n";
			$string .= "            ->find();\n";
			$string .= "        return \$". $table['table'] .";\n";
			$string .= "    }\n";
			$string .= "\n";
			$string .= "    public static function add(\$param)\n";
			$string .= "    {\n";
			$string .= "        \$id = Db::name('". $table['table'] ."')->insertGetId(\$param);\n";
			$string .= "\n";
			$string .= "        if (empty(\$id)) {\n";
			$string .= "            exception();\n";
			$string .= "        }\n";
			$string .= "\n";
			$string .= "        \$param['id'] = \$id;\n";
			$string .= "\n";
			$string .= "        return \$param;\n";
			$string .= "    }\n";
			$string .= "\n";
			$string .= "    \n";
			$string .= "    public static function edit(\$param)\n";
			$string .= "    {\n";
			$string .= "        \$id = \$param['id'];\n";
			$string .= "        \$res = Db::name('". $table['table'] ."')\n";
			$string .= "            ->where('id', \$id)\n";
			$string .= "            ->update(\$param);\n";
			$string .= "\n";
			$string .= "        if (empty(\$res)) {\n";
			$string .= "            exception();\n";
			$string .= "        }\n";
			$string .= "\n";
			$string .= "        \$param['id'] = \$id;\n";
			$string .= "\n";
			$string .= "        return \$param;\n";
			$string .= "    }\n";
			$string .= "\n";
			$string .= "    public static function del(\$id)\n";
			$string .= "    {\n";
			$string .= "        Db::name('". $table['table'] ."')->delete(\$id);\n";
			$string .= "        return \$id;\n";
			$string .= "    }\n";
			$string .= "\n";
			$string .= "}\n";
       
            if ($handle = fopen($file, 'w')) {
                fwrite($handle, $string);
                fclose($handle);
            } else {
                throw new \think\Exception(__('File {:file} does not have {:type} permission', ['file'=>$file, 'type' => 'write']), '500');
            }

            //生成控制器
            $file = $tablePath .'\\' . $tableName .'.php';
            $string = "";

            $string .= "<?php\n";
			$string .= "namespace app\\". $appName ."\\controller;\n";
			$string .= "\n";
			$string .= "use think\\facade\\Request;\n";
			$string .= "use app\\BaseController;\n";
			$string .= "use app\\". $appName ."\\model\\" . $tableName ."Model;\n";
			$string .= "use app\\". $appName ."\\service\\" . $tableName ."Service;\n";
			$string .= "use app\\". $appName ."\\validate\\" . $tableName ."Validate;\n";
			$string .= "use hg\\apidoc\\annotation as Apidoc;\n";
			$string .= "\n";
			$string .= "/**\n";
			$string .= " * @Apidoc\\Title(\"". $table['title'] ."\")\n";
			$string .= " * @Apidoc\\Group(\"". $appName ."\")\n";
			$string .= " */\n";
			$string .= "class ". $tableName ." extends BaseController\n";
			$string .= "{\n";
			$string .= "    /**\n";
			$string .= "     * @Apidoc\\Title(\"列表\")\n";
			$string .= "     * @Apidoc\\Param(\"page\", type=\"int\", default=\"1\", desc=\"页码\")\n";
			$string .= "     * @Apidoc\\Param(\"limit\", type=\"int\", default=\"10\", desc=\"pagesize\")\n";
			$string .= "     * @Apidoc\\Param(\"sort_field\", type=\"string\", default=\"\", desc=\"sort_field\")\n";
			$string .= "     * @Apidoc\\Param(\"sort_type\", type=\"string\", default=\"\", desc=\"sort_type\")\n";
			$string .= "     * @Apidoc\\Returned(ref=\"return\"),\n";
			$string .= "     * @Apidoc\\Returned(\"data\", type=\"object\", desc=\"返回数据\",\n";
			$string .= "     *      @Apidoc\\Returned(ref=\"returnPaging\"),\n";
			$string .= "     *      @Apidoc\\Returned(\"list\", type=\"array\", desc=\"数据列表\")\n";
			$string .= "     * )\n";
			$string .= "     */\n";
			$string .= "    public function list()\n";
			$string .= "    {\n";
			$string .= "        \$page       = Request::param('page/d', 1);\n";
			$string .= "        \$limit      = Request::param('limit/d', 5);\n";
			$string .= "        \$sort_field = Request::param('sort_field/s ', '');\n";
			$string .= "        \$sort_type  = Request::param('sort_type/s', '');\n";
			$string .= "\n";
            $string .= "        \$where = [];\n";
            foreach($table['fields'] as $field)
            {
                if($field['search_type'] !='_')
                {
                    $string .= "        \$".$field['field']."               = Request::param('".$field['field']."/s', '');\n";
                    $string .= "        if (\$".$field['field'].") {\n";
                    $string .= "            \$where[] = ['".$field['field']."', '".$field['search']."',\$".$field['field']."];\n";
                    $string .= "        }\n";
                }
            }
			$string .= "\n";
			$string .= "        \$order = [];\n";
			$string .= "        if (\$sort_field && \$sort_type) {\n";
			$string .= "            \$order = [\$sort_field => \$sort_type];\n";
			$string .= "        }\n";
			$string .= "        \$data = ". $tableName ."Service::list(\$where, \$page, \$limit, \$order);\n";
			$string .= "\n";
			$string .= "        return success(\$data);\n";
			$string .= "    }\n";
			$string .= "\n";
			$string .= "    /**\n";
			$string .= "     * @Apidoc\\Title(\"信息\")\n";
			$string .= "     * @Apidoc\\Param(\"id\", type=\"int\", default=\"1\", desc=\"id\")\n";
			$string .= "     * @Apidoc\\Returned(ref=\"return\")\n";
			$string .= "     */\n";
			$string .= "    public function info()\n";
			$string .= "    {\n";
			$string .= "        \$param['id'] = Request::param('id/d', '');\n";
			$string .= "        validate(". $tableName ."Validate::class)->scene('info')->check(\$param);\n";
			$string .= "        \$data = ". $tableName ."Service::info(\$param['id']);\n";
			$string .= "\n";
			$string .= "        return success(\$data);\n";
			$string .= "    }\n";
			$string .= "\n";
			$string .= "    /**\n";
			$string .= "     * @Apidoc\\Title(\"添加\")\n";
			$string .= "     * @Apidoc\\Method(\"POST\")\n";
            foreach($table['fields'] as $field)
            {
                $string .= "     * @Apidoc\\Param(\"".$field['field']."\", type=\"string\", default=\"\", desc=\"".$field['title']."\")\n";
            }   
			$string .= "     * @Apidoc\\Returned(ref=\"return\")\n";
			$string .= "     */\n";
			$string .= "    public function add()\n";
			$string .= "    {\n";
            foreach($table['fields'] as $field)
            {
                if($field['field']=='id') continue;
                $string .= "        \$param['".$field['field']."'] \t\t\t= Request::param('".$field['field']."/s', '');\n";
            } 
			$string .= "        validate(". $tableName ."Validate::class)->scene('add')->check(\$param);\n";
			$string .= "        \$data = ". $tableName ."Service::add(\$param);\n";
			$string .= "\n";
			$string .= "        return success(\$data);\n";
			$string .= "    }\n";
			$string .= "\n";
			$string .= "    /**\n";
			$string .= "     * @Apidoc\\Title(\"修改\")\n";
			$string .= "     * @Apidoc\\Method(\"POST\")\n";
			$string .= "     * @Apidoc\\Header(ref=\"headerAdmin\")\n";
            foreach($table['fields'] as $field)
            {
                $string .= "     * @Apidoc\\Param(\"".$field['field']."\", type=\"string\", default=\"\", desc=\"".$field['title']."\")\n";
            }
			$string .= "     * @Apidoc\\Returned(ref=\"return\")\n";
			$string .= "     */\n";
			$string .= "    public function edit()\n";
			$string .= "    {\n";
            foreach($table['fields'] as $field)
            {
                $string .= "        \$param['".$field['field']."']\t\t\t= Request::param('".$field['field']."/s', '');\n";
            } 
			$string .= "\n";
			$string .= "        validate(". $tableName ."Validate::class)->scene('edit')->check(\$param);\n";
			$string .= "\n";
			$string .= "        \$data = ". $tableName ."Service::edit(\$param);\n";
			$string .= "\n";
			$string .= "        return success(\$data);\n";
			$string .= "    }\n";
			$string .= "\n";
			$string .= "        /**\n";
			$string .= "     * @Apidoc\\Title(\"删除\")\n";
			$string .= "     * @Apidoc\\Method(\"POST\")\n";
			$string .= "     * @Apidoc\\Header(ref=\"headerAdmin\")\n";
			$string .= "     * @Apidoc\\Param(\"id\", type=\"int\", default=\"1\", desc=\"id\")\n";
			$string .= "     * @Apidoc\\Returned(ref=\"return\")\n";
			$string .= "     */\n";
			$string .= "    public function del()\n";
			$string .= "    {\n";
			$string .= "        \$param['id'] = Request::param('id/d', '');\n";
			$string .= "\n";
			$string .= "        validate(". $tableName ."Validate::class)->scene('del')->check(\$param);\n";
			$string .= "\n";
			$string .= "        \$data = ". $tableName ."Service::del(\$param['id']);\n";
			$string .= "\n";
			$string .= "        return success(\$data);\n";
			$string .= "    }\n";
			$string .= "}\n";
			$string .= "\n";

            if ($handle = fopen($file, 'w')) {
                fwrite($handle, $string);
                fclose($handle);
            } else {
                throw new \think\Exception(__('File {:file} does not have {:type} permission', ['file'=>$file, 'type' => 'write']), '500');
            }

            //生成api js文件
            $file = $tablePath .'\\' . $table['table'] .'.js';
            $string = "";

            $string .= "import request from '@/utils/request'\n";
			$string .= "\n";
			$string .= "export function list(params) {\n";
			$string .= "  return request({\n";
			$string .= "    url: '/". $appName ."/". $tableName ."/list',\n";
			$string .= "    method: 'get',\n";
			$string .= "    params: params\n";
			$string .= "  })\n";
			$string .= "}\n";
			$string .= "\n";
			$string .= "export function info(params) {\n";
			$string .= "  return request({\n";
			$string .= "    url: '/". $appName ."/". $tableName ."/info',\n";
			$string .= "    method: 'get',\n";
			$string .= "    params: params\n";
			$string .= "  })\n";
			$string .= "}\n";
			$string .= "\n";
			$string .= "export function add(data) {\n";
			$string .= "  return request({\n";
			$string .= "    url: '/". $appName ."/". $tableName ."/add',\n";
			$string .= "    method: 'post',\n";
			$string .= "    data\n";
			$string .= "  })\n";
			$string .= "}\n";
			$string .= "\n";
			$string .= "export function edit(data) {\n";
			$string .= "  return request({\n";
			$string .= "    url: '/". $appName ."/". $tableName ."/edit',\n";
			$string .= "    method: 'post',\n";
			$string .= "    data\n";
			$string .= "  })\n";
			$string .= "}\n";
			$string .= "\n";
			$string .= "export function del(data) {\n";
			$string .= "  return request({\n";
			$string .= "    url: '/". $appName ."/". $tableName ."/del',\n";
			$string .= "    method: 'post',\n";
			$string .= "    data\n";
			$string .= "  })\n";
			$string .= "}\n";

            if ($handle = fopen($file, 'w')) {
                fwrite($handle, $string);
                fclose($handle);
            } else {
                throw new \think\Exception(__('File {:file} does not have {:type} permission', ['file'=>$file, 'type' => 'write']), '500');
            }

            //生成vue文件
            $file = $tablePath .'\\' . $table['table'] .'.vue';
            $string = "";

            $string .= "<template>\n";
			$string .= "  <div class=\"app-container\">\n";
			$string .= "    <!-- 查询 -->\n";
			$string .= "    <div class=\"filter-container\">\n";
			$string .= "      <el-row :gutter=\"0\">\n";
			$string .= "        <el-col :xs=\"24\" :sm=\"22\">\n";
			foreach($table['fields'] as $field)
            {
                if($field['search_type'] !='_')
                    $string .= "          <el-input v-model=\"query.".$field['field']."\" class=\"filter-item\" style=\"width: 200px;\" placeholder=\"".$field['title']."\" clearable />\n";
            } 
			$string .= "          <el-button class=\"filter-item\" type=\"primary\" @click=\"search()\">查询</el-button>\n";
			$string .= "          <el-button class=\"filter-item\" @click=\"refresh()\">刷新</el-button>\n";
			$string .= "        </el-col>\n";
			$string .= "        <el-col :xs=\"24\" :sm=\"2\" style=\"text-align:right;\">\n";
			$string .= "          <el-button class=\"filter-item\" type=\"primary\" @click=\"add()\">添加</el-button>\n";
			$string .= "        </el-col>\n";
			$string .= "      </el-row>\n";
			$string .= "    </div>\n";
			$string .= "    <!-- 列表 -->\n";
			$string .= "    <el-table v-loading=\"loading\" :data=\"datas\" :height=\"height\" style=\"width: 100%\" border @sort-change=\"sort\">\n";
            foreach($table['fields'] as $field)
            {
                $string .= "      <el-table-column prop=\"".$field['field']."\" label=\"".$field['title']."\" min-width=\"100\" sortable=\"custom\"/>\n";
			}
			$string .= "      <el-table-column label=\"操作\" min-width=\"100\" align=\"right\" fixed=\"right\">\n";
			$string .= "        <template slot-scope=\"{ row }\">\n";
			$string .= "          <el-button size=\"mini\" type=\"success\" @click=\"edit(row)\">修改</el-button>\n";
			$string .= "          <el-button size=\"mini\" type=\"danger\" @click=\"del(row)\">删除</el-button>\n";
			$string .= "        </template>\n";
			$string .= "      </el-table-column>\n";
			$string .= "    </el-table>\n";
			$string .= "    <!-- 分页 -->\n";
			$string .= "    <pagination v-show=\"count > 0\" :total=\"count\" :page.sync=\"query.page\" :limit.sync=\"query.limit\" @pagination=\"list\" />\n";
			$string .= "    <!-- 添加、修改 -->\n";
			$string .= "    <el-dialog :title=\"dialogTitle\" :visible.sync=\"dialog\" top=\"1vh\" :before-close=\"cancel\">\n";
			$string .= "      <el-form ref=\"ref\" :rules=\"rules\" :model=\"model\" label-width=\"100px\" class=\"dialog-body\" :style=\"{height:height+30+'px'}\">\n";
			foreach($table['fields'] as $field)
            {
				if($field['field']=='id')continue;
                $string .= "        <el-form-item label=\"".$field['title']."\" prop=\"".$field['field']."\">\n";
                $string .= "          <el-input v-model=\"model.".$field['field']."\" placeholder=\"请输入".$field['title']."\" clearable />\n";
                $string .= "        </el-form-item>\n";
            }
			$string .= "      </el-form>\n";
			$string .= "      <div slot=\"footer\" class=\"dialog-footer\">\n";
			$string .= "        <el-button @click=\"cancel\">取消</el-button>\n";
			$string .= "        <el-button type=\"primary\" @click=\"submit\">提交</el-button>\n";
			$string .= "      </div>\n";
			$string .= "    </el-dialog>\n";
			$string .= "  </div>\n";
			$string .= "</template>\n";
			$string .= "\n";
			$string .= "<script>\n";
			$string .= "import screenHeight from '@/utils/screen-height'\n";
			$string .= "import Pagination from '@/components/Pagination'\n";
			$string .= "import { list, info, add, edit, del } from '@/api/".$table['table']."'\n";
			$string .= "\n";
			$string .= "export default {\n";
			$string .= "  name: 'Role',\n";
			$string .= "  components: { Pagination },\n";
			$string .= "  data() {\n";
			$string .= "    return {\n";
			$string .= "      height: 680,\n";
			$string .= "      loading: false,\n";
			$string .= "      datas: [],\n";
			$string .= "      count: 0,\n";
			$string .= "      query: {\n";
			$string .= "        page: 1,\n";
			$string .= "        limit: 13\n";
			$string .= "      },\n";
			$string .= "      dialog: false,\n";
			$string .= "      dialogTitle: '',\n";
			$string .= "      model: {\n";
            foreach($table['fields'] as $field)
            {
                $string .= "        ".$field['field'].":'',\n";
            }
			$string .= "      },\n";
			$string .= "      rules: {\n";
			$string .= "        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],\n";
			$string .= "      },\n";
			$string .= "    }\n";
			$string .= "  },\n";
			$string .= "  created() {\n";
			$string .= "    this.height = screenHeight()\n";
			$string .= "    this.list()\n";
			$string .= "  },\n";
			$string .= "  methods: {\n";
			$string .= "    // 列表\n";
			$string .= "    list() {\n";
			$string .= "      this.loading = true\n";
			$string .= "      list(this.query).then(res => {\n";
			$string .= "        this.datas = res.data.list\n";
			$string .= "        this.count = res.data.count\n";
			$string .= "        this.loading = false\n";
			$string .= "      }).catch(() => {\n";
			$string .= "        this.loading = false\n";
			$string .= "      })\n";
			$string .= "    },\n";
			$string .= "    // 添加\n";
			$string .= "    add() {\n";
			$string .= "      this.dialog = true\n";
			$string .= "      this.dialogTitle = '添加'\n";
			$string .= "      this.reset()\n";
			$string .= "    },\n";
			$string .= "    edit(row) {\n";
			$string .= "      this.dialog = true\n";
			$string .= "      this.dialogTitle = '修改：' + row.id\n";
			$string .= "      info({\n";
			$string .= "        id: row.id\n";
			$string .= "      }).then(res => {\n";
			$string .= "        this.reset(res.data)\n";
			$string .= "      })\n";
			$string .= "    },\n";
			$string .= "    // 删除\n";
			$string .= "    del(row) {\n";
			$string .= "      this.\$confirm(\n";
			$string .= "        '确定要删除 <span style=\"color:red\">' + row.name + ' </span>吗？',\n";
			$string .= "        '删除：' + row.id,\n";
			$string .= "        { type: 'warning', dangerouslyUseHTMLString: true }\n";
			$string .= "      ).then(() => {\n";
			$string .= "        this.loading = true\n";
			$string .= "        del({\n";
			$string .= "          id: row.id\n";
			$string .= "        }).then(res => {\n";
			$string .= "          this.reset()\n";
			$string .= "          this.list()\n";
			$string .= "          this.\$message.success(res.msg)\n";
			$string .= "        }).catch(() => {\n";
			$string .= "          this.loading = false\n";
			$string .= "        })\n";
			$string .= "      }).catch(() => {})\n";
			$string .= "    },\n";
			$string .= "    // 取消\n";
			$string .= "    cancel() {\n";
			$string .= "      this.dialog = false\n";
			$string .= "      this.reset()\n";
			$string .= "    },\n";
			$string .= "    // 提交\n";
			$string .= "    submit() {\n";
			$string .= "      this.\$refs['ref'].validate(valid => {\n";
			$string .= "        if (valid) {\n";
			$string .= "          this.loading = true\n";
			$string .= "          if (this.model.id) {\n";
			$string .= "            edit(this.model).then(res => {\n";
			$string .= "              this.list()\n";
			$string .= "              this.reset()\n";
			$string .= "              this.dialog = false\n";
			$string .= "              this.\$message.success(res.msg)\n";
			$string .= "            }).catch(() => {\n";
			$string .= "              this.loading = false\n";
			$string .= "            })\n";
			$string .= "          } else {\n";
			$string .= "            add(this.model).then(res => {\n";
			$string .= "              this.list()\n";
			$string .= "              this.reset()\n";
			$string .= "              this.dialog = false\n";
			$string .= "              this.\$message.success(res.msg)\n";
			$string .= "            }).catch(() => {\n";
			$string .= "              this.loading = false\n";
			$string .= "            })\n";
			$string .= "          }\n";
			$string .= "        }\n";
			$string .= "      })\n";
			$string .= "    },\n";
			$string .= "    // 重置\n";
			$string .= "    reset(row) {\n";
			$string .= "      if (row) {\n";
			$string .= "        this.model = row\n";
			$string .= "      } else {\n";
			$string .= "        this.model = this.\$options.data().model\n";
			$string .= "      }\n";
			$string .= "      if (this.\$refs['ref'] !== undefined) {\n";
			$string .= "        this.\$refs['ref'].resetFields()\n";
			$string .= "      }\n";
			$string .= "    },\n";
			$string .= "    // 查询\n";
			$string .= "    search() {\n";
			$string .= "      this.query.page = 1\n";
			$string .= "      this.list()\n";
			$string .= "    },\n";
			$string .= "    // 刷新\n";
			$string .= "    refresh() {\n";
			$string .= "      this.query = this.\$options.data().query\n";
			$string .= "      this.list()\n";
			$string .= "    },\n";
			$string .= "    sort(sort) {\n";
			$string .= "      this.query.sort_field = sort.prop\n";
			$string .= "      this.query.sort_type = ''\n";
			$string .= "      if (sort.order === 'ascending') {\n";
			$string .= "        this.query.sort_type = 'asc'\n";
			$string .= "        this.list()\n";
			$string .= "      }\n";
			$string .= "      if (sort.order === 'descending') {\n";
			$string .= "        this.query.sort_type = 'desc'\n";
			$string .= "        this.list()\n";
			$string .= "      }\n";
			$string .= "    },\n";
			$string .= "  }\n";
			$string .= "}\n";
			$string .= "</script>\n";
			$string .= "\n";
			$string .= "<style scoped>\n";
			$string .= ".custom-tree-node {\n";
			$string .= "  flex: 1;\n";
			$string .= "  display: flex;\n";
			$string .= "  align-items: center;\n";
			$string .= "  justify-content: space-between;\n";
			$string .= "  font-size: 14px;\n";
			$string .= "  padding-right: 8px;\n";
			$string .= "}\n";
			$string .= "</style>\n";
			$string .= "\n";

            if ($handle = fopen($file, 'w')) {
                fwrite($handle, $string);
                fclose($handle);
            } else {
                throw new \think\Exception(__('File {:file} does not have {:type} permission', ['file'=>$file, 'type' => 'write']), '500');
            }
        }
        return json($list);
    }

    public function read()
    {
        $tablePath = Config::get('curd.engine.file.save_path');
        if (!is_dir($tablePath)) {
            mkdir($tablePath, 0777, true);
        }
        $tablePath= $tablePath . 'template';
        if (!is_dir($tablePath)) {
            mkdir($tablePath, 0777, true);
        }

        $infileName = $tablePath .'\\in.txt';
        $infile = fopen($infileName, "r") or die("Unable to open file!");
        $outfileName = $tablePath .'\\out.txt';
        $outfile = fopen($outfileName, "w") or die("Unable to open file!");
        while(! feof($infile))
        {
            $txt = fgets($infile);
            $txt = str_replace("\\","\\\\",$txt);
            $txt = str_replace("\n","",$txt);
            $txt = str_replace("\r","",$txt);
            $txt = str_replace("$","\\$",$txt);
            $txt = str_replace("\"","\\\"",$txt);
            $txt= "\t\t\t$"."string .= \"" . $txt . "\\n\";\n";
            fputs($outfile,$txt);
        }
        fclose($infile);
        fclose($outfile);
    }

    
}
